<?php


namespace models;


use think\Model;

class Category extends Model
{

    public function check_catid($catid, $module_category)
    {
        if ($module_category == '*') return true;
        return in_array($catid, $module_category);
    }

    public function getCatTree($arr, $id = 0, $lev = 0)
    {
        $tree = array();
        foreach ($arr as $v) {
            if ($v['parentid'] == $id) {
                $v['lev'] = $lev;
                $tree[] = $v;
                $tree = array_merge($tree, $this->getCatTree($arr, $v['catid'], $lev + 1));
            }
        }
        return $tree;
    }

    public function category_select($moduleid)
    {
        $all_cats = \think\facade\Db::name('category')->where('moduleid', $moduleid)->order('listorder asc')->select()->toArray();
        return $this->getCatTree($all_cats);
    }

    protected function get_arrparentid($categorys, $catid, $arrparentid = '', $n = 1)
    {
        if ($n > 10 || !isset($this->categorys[$catid])) return "0";

        $parentid = $categorys[$catid]['parentid'];
        $arrparentid = $arrparentid ? $parentid . ',' . $arrparentid : $parentid;
        if ($parentid > 0) {
            $arrparentid = $this->get_arrparentid($categorys, $parentid, $arrparentid, $n + 1);
        }
        return $arrparentid;
    }

    protected function get_arrchildid($categorys, $catid)
    {
        $arrchildid = $catid;

        foreach ($categorys as $id => $cat) {
            if ($cat['parentid'] && $id != $catid && $cat['parentid'] == $catid) {
                $arrchildid .= ',' . $this->get_arrchildid($categorys, $id);
            }
        }

        return $arrchildid;
    }

    public function cache($moduleid)
    {

        $categorys = \think\facade\Db::name('category')->where(array('moduleid' => $moduleid))->order('listorder asc')->column('*', 'catid');

        foreach ($categorys as $catid => $cat) {
            $arrparentid = $this->get_arrparentid($categorys, $catid);
            $arrchildid = $this->get_arrchildid($categorys, $catid);
            $child = strpos($arrchildid, ',') === false ? 0 : 1;
            $tmp = [];
            $tmp = array_merge($tmp, $cat);
            $tmp['arrparentid'] = $arrparentid;
            $tmp['arrchildid'] = $arrchildid;
            $tmp['child'] = $child;
            $tmp['cat_pos'] = $this->cat_pos($tmp);

            \think\facade\Db::name('category')->field('arrparentid,arrchildid,child,cat_pos')->where('catid', $catid)->update($tmp);
        }

        return true;
    }

    public function get_category($catid)
    {
        $catid = intval($catid);
        return \think\facade\Db::name('category')->where(array('catid' => $catid))->find();
    }

    public function cat_pos($CAT, $str = ' &raquo; ')
    {
        if (!$CAT) return '';

        if ($CAT['arrparentid'] == 0) {
            return $CAT['catname'];
        }

        $arrparentid = $CAT['arrparentid'] . ',' . $CAT['catid'];
        $arrparentids = explode(',', $arrparentid);

        $CATEGORY = [];
        $ddd = \think\facade\Db::name('category')->where(" catid IN ($arrparentids) ")->select()->toArray();

        foreach ($ddd as $r) {
            $CATEGORY[$r['catid']] = $r['catname'];
        }

        $pos = [];

        foreach ($arrparentids as $index => $catid) {
            if (!$catid || !isset($CATEGORY[$catid])) continue;

            $pos[] = $CATEGORY[$catid];
        }

        return implode($str, $pos);
    }

    public function max_catid()
    {
        $catid = \think\facade\Db::name('category')->max('catid');
        if (!$catid) {
            return 0;
        }
        return intval($catid);
    }


    public function mod_check_ok($moduleid, $catid, $parentid)
    {
        if ($catid == $parentid) {
            return false;
        }
        $all_cats = \think\facade\Db::name('category')->where(array('moduleid' => $moduleid))->order('listorder asc')->select()->toArray();
        $sub_cats = $this->getCatTree($all_cats, $catid);
        $ids = [];
        foreach ($sub_cats as $v) {
            $ids[] = $v['catid'];
        }
        #判断所选的父分类是否为当前分类或其后代分类
        if (in_array($parentid, $ids)) return false;
        return true;
    }

    public function del_check_ok($catid, $moduleid)
    {
        $r = \think\facade\Db::name('category')->field('catid')->where(array('parentid' => $catid))->find();
        if ($r) return false;

        $menu = \Menu::get($moduleid);
        $r = \think\facade\Db::name($menu['module_en'])->where(array('catid' => $catid))->count();
        if ($r) return false;

        return true;
    }

    //处理编辑时的垃圾附件
    public function treat_attachment($pk)
    {
        $data = \think\facade\Db::name('category')->where(['catid' => $pk])->find();
        $string = $data['thumb'];

        $attachments = \think\facade\Db::name('category_attachment')->where(['catid' => $pk])->select()->toArray();
        foreach ($attachments as $k => $v) {
            if (strpos($string, $v['url']) !== false) continue;

            $file = root_path() . 'public' . $v['url'];
            if (!file_exists($file)) continue;
            unlink($file);
            \think\facade\Db::name('category_attachment')->where(['catid' => $v['catid']])->delete();
        }
        return true;
    }

    //删除附件
    public function clear_attachment($pk)
    {
        $attachments = \think\facade\Db::name('category_attachment')->where(['catid' => $pk])->select()->toArray();
        foreach ($attachments as $k => $v) {
            $file = root_path() . 'public' . $v['url'];
            if (!file_exists($file)) continue;
            unlink($file);
        }
        \think\facade\Db::name('category_attachment')->where(['catid' => $pk])->delete();
    }

}